package com.example.demo01.utils;

import java.io.*;
import java.net.*;
import java.nio.file.Files;
import java.nio.file.Path;

public class FileUtil {
    public static boolean copyFile(String sourcePath, String destPath) {
        boolean flag = false;
        File source = new File(sourcePath);
        File dest = new File(destPath);
        try {
            Files.copy(source.toPath(), dest.toPath());
            flag = true;
        } catch (IOException e) {
            throw new RuntimeException(e);

        }
        return flag;
    }

    /**
     * @param remotePath 服务器目录
     * @param fileName   文件名
     * @打印服务器文件
     */
    public static void printFile(String remotePath, String fileName) {
        InputStream inputStream = getInputStreamByUrl(remotePath, fileName);
        try (BufferedInputStream bufferedInput = new BufferedInputStream(inputStream);) {
            byte[] buffer = new byte[1024];
            int bytesRead = 0;
            StringBuilder bu = new StringBuilder();
            while ((bytesRead = bufferedInput.read(buffer)) != -1) {
                String chunk = new String(buffer, 0, bytesRead);
                bu.append(chunk);
            }
            System.out.print("======================" + bu);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @param remotePath
     * @param fileName
     * @return
     * @请求方式requestMethod 默认GET
     */
    public static InputStream getInputStreamByUrl(String remotePath, String fileName) {
        return getInputStreamByUrl(remotePath, fileName, null);
    }

    public static InputStream getInputStreamByUrlPOST(String remotePath, String fileName) {
        return getInputStreamByUrl(remotePath, fileName, "POST");
    }

    public static InputStream getInputStreamByUrl(String remotePath, String fileName, String requestMethod) {
        //服务器上文件路径
        String urlPath = remotePath + fileName;
        try {
            URL url = new URL(urlPath);
            // http的连接类
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            //http的连接类设置超时、编码格式等
            URLConnectionSets(conn, requestMethod);

            //获得输入流
            return conn.getInputStream();
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static OutputStream getOutputStreamByUrl(String remotePath, String fileName, String requestMethod) {
        //服务器上文件路径
        String urlPath = remotePath + fileName;
        try {
            URL url = new URL(urlPath);
            // http的连接类
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            //http的连接类设置超时、编码格式等
            URLConnectionSets(conn, requestMethod);
            conn.setDoOutput(true);
            conn.setDoInput(true);

            //获得输入流
            return conn.getOutputStream();
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @param remotePath
     * @param fileName
     * @return
     * @请求方式requestMethod 默认GET
     */
    public static OutputStream getOutputStreamByUrl(String remotePath, String fileName) {
        return getOutputStreamByUrl(remotePath, fileName, null);
    }

    public static OutputStream getOutputStreamByUrlPOST(String remotePath, String fileName) {
        return getOutputStreamByUrl(remotePath, fileName, "POST");
    }

    private static void URLConnectionSets(HttpURLConnection conn, String requestMethod) {
        //设置超时
        conn.setConnectTimeout(1000 * 5);//5秒

        // 设置字符编码
        conn.setRequestProperty("Charset", "UTF-8");

        //设置请求方式，默认是GET
        if ("POST".equals(requestMethod)) {
            try {
                conn.setRequestMethod("POST");
            } catch (ProtocolException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static boolean copyFile(InputStream ins, OutputStream ous) {
        boolean flag = false;
        try (BufferedInputStream bis = new BufferedInputStream(ins);
             BufferedOutputStream bos = new BufferedOutputStream(ous);) {
            int temp;
            while ((temp = bis.read()) > 0) {
                bos.write(temp);
            }

//            byte[] bytes = new byte[1024];
//            int temp = 0;
//            if ((temp = ins.read(bytes)) != -1) {
//                ous.write(bytes, 0, temp);
//            }
//            ous.flush();
            flag = true;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return flag;
    }

    public static boolean copyFile2(String from, String to) {
        boolean flag = false;
        try ( //创建输入流
              InputStream fis = new FileInputStream(from);
              //创建输出流
              OutputStream fos = new FileOutputStream(to);
        ) {
            //一边读，一边写
            byte[] bytes = new byte[1024]; //1KB
            int temp = 0;
            while ((temp = fis.read(bytes)) != -1) {
                fos.write(bytes, 0, temp);
            }
            //刷新
            fos.flush();
            flag = true;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return flag;
    }
}
